<?php
if(!defined('PATH')) exit();

class Highscores extends Controller {

	protected $default_action = 'experience';

	public $_title = 'Highscores';

	protected $_config = array(
		'limit_per_page' => null,
		);

	public function __construct() {
		parent::__construct();
		$this->_config['limit_per_page'] = $this->config['controller'][strtolower(get_class($this))]['limit_per_page'];
	}

	public function action_experience() {
		$this->view();
	}

	public function action_magic() {
		$this->view();
	}
	public function action_fist() {
		$this->view();
	}
	public function action_sword() {
		$this->view();
	}
	public function action_club() {
		$this->view();
	}

	public function action_axe() {
		$this->view();
	}

	public function action_distance() {
		$this->view();
	}

	public function action_shielding() {
		$this->view();
	}

	public function action_fishing() {
		$this->view();
	}

	public function view() {
		$number_of_players = $this->db->query('SELECT COUNT(*) as `n` FROM `players` WHERE `players`.`deleted` = 0 AND `players`.`group_id` < 2 AND `account_id` != 1')->fetch();
		$number_of_players = $number_of_players[0];
		$max_page = (int) ceil(((float)$number_of_players)/((float)$this->_config['limit_per_page']));
		$offset = 0;
		$page = 1;
		if (isset($this->paramsURI[2])) {
			$page = $this->paramsURI[2];
			if ($page > $max_page || $page < 1)
				$this->redirect(PATH.'/'.strtolower(get_class($this)).'/'.$this->paramsURI[1]);
			$offset = ($page-1)*$this->_config['limit_per_page'];
		}

		if($this->paramsURI[1] == 'experience')
		{
			$list = $this->db->query('SELECT `level` as `value`, `name`, `online` FROM `players` WHERE `players`.`deleted` = 0 AND `players`.`group_id` < 2 AND `account_id` != 1 ORDER BY `experience` DESC LIMIT '.$this->_config['limit_per_page'].' OFFSET '.$offset);
		}
		elseif($this->paramsURI[1] == 'magic')
		{
			$list = $this->db->query('SELECT `maglevel` as `value`, `name`, `online` FROM `players` WHERE `players`.`deleted` = 0 AND `players`.`group_id` < 2 AND `account_id` != 1 ORDER BY `maglevel` DESC, `manaspent` DESC LIMIT '.$this->_config['limit_per_page'].' OFFSET '.$offset);
		}
		else
		{
			$list = $this->db->query('SELECT `skillid`, `value`, `name`, `online` FROM `players`, `player_skills` WHERE `players`.`deleted` = 0 AND `players`.`group_id` < 2 AND `players`.`id` = `player_skills`.`player_id` AND `player_skills`.`skillid` = '.$this->archez->getSkillId($this->paramsURI[1]).' AND `players`.`account_id` != 1 ORDER BY value DESC, count DESC LIMIT '.$this->_config['limit_per_page'].' OFFSET '.$offset);
		}

		$view = new View('highscores/index');
		$view->list = $list;
		$view->next_page = true;
		$view->prev_page = false;
		if ($page == $max_page || $max_page == 0)
			$view->next_page = false;
		if ($page > 1)
			$view->prev_page = true;
		$view->page = $page;
		$view->offset = $offset;
		$view->render();
	}
}
?>